#include "applnk.h"
#include <QDebug>
#include <QPainter>
#include <QToolTip>
#include <QProcess>
#include <QMessageBox>
#include <QFileIconProvider>
#include <QMouseEvent>
#include <QPropertyAnimation>

#include "settinghandler.h"
#include "widget.h"

const int TIME500 = 500;

AppLNK::AppLNK(const QString &lnk_name, const QString& lnk_dir, QWidget *parent)
    : QPushButton(parent)
{
    qDebug() << "AppLNK" << lnk_name << "创建了";

    // 初始控件大小和图标label大小
    auto my_size = QSize(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());
    resize(my_size);

    // 处理名称，保证不以.lnk结尾
    lnk_name_ = lnk_name;
    if (lnk_name_.endsWith(".lnk"))
        lnk_name_ = lnk_name_.mid(0, lnk_name_.size()-4);

    // 根据快捷方式获取绝对路径
    QString temp_path = lnk_dir + lnk_name_ + ".lnk";

    auto lnk_file_info = QFileInfo(temp_path);
    canonical_filename = lnk_file_info.canonicalFilePath().section('/', -1);
    filedir = lnk_file_info.canonicalPath();

    // 获取图标文件名
//    qDebug() << get_name();
//    qDebug() << get_absolute_path();
//    qDebug() << get_absolute_dir();

    // 判断该图标文件是否存在
    bool icon_file_exist = false;
    if (canonical_filename.endsWith(".exe"))  // 是exe文件才判断自定义的图标文件是否存在
        icon_file_exist = QFileInfo(Widget::get_icons_dir() + get_name() + ".png").isFile();

    QIcon ic;
    if (icon_file_exist)
    {
        ic = QIcon(Widget::get_icons_dir() + get_name() + ".png");
    }
    else  // 如果没有找到图标文件，就加载默认图标
    {
//        QMessageBox::information(this, "", get_absolute_path() + "\n" + lnk_file_info.canonicalFilePath());
        QFileInfo file_info = QFileInfo(lnk_file_info.canonicalFilePath());
        ic = QFileIconProvider().icon(file_info).pixmap(my_size);
        // 保存一下获取的默认图标，便于后面可以变换大小
        system_icon = QFileIconProvider().icon(file_info).pixmap(my_size);
    }

    setFlat(true);
    setIcon(ic);
    setIconSize(my_size);
}

void AppLNK::set_size(const int &wd, const int &ht)
{
    auto new_size = QSize(wd, ht);
    resize(new_size);
    setIconSize(new_size);
    if (! system_icon.isNull())  // 如果图标是获取的系统默认图标
    {
        // 这样写还是会泄露
        system_icon = system_icon.scaled(new_size);
        setIcon(system_icon);
        // 以下写法导致内存泄露
//        setIcon(system_icon.scaled(new_size));
    }
}

void AppLNK::mousePressEvent(QMouseEvent *event)
{
    is_clicked = true;

    // 变大
    move(old_x - SettingHandler::get_instance()->get_border_size(), old_y - SettingHandler::get_instance()->get_border_size());
    set_size(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height());

    auto pos = event->pos();
    // 用下面两个变量保存鼠标按下时，相对窗口左上角的点的位置
    press_x = pos.x();
    press_y = pos.y();
}

void AppLNK::mouseReleaseEvent(QMouseEvent *event)
{
    // 还原
    move(old_x, old_y);
    set_size(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());

    if (is_clicked)
    {
        if (event->button() == Qt::LeftButton)
            emit left_clicked_me();
        else if (event->button() == Qt::RightButton)
            emit right_clicked_me();
    }
    else
    {
        auto mouse_pos = event->scenePosition();  // 相对于父窗口的坐标
        is_change_index = true;
        emit change_index(mouse_pos.x(), mouse_pos.y());
    }
}

void AppLNK::mouseMoveEvent(QMouseEvent *event)
{
    is_clicked = false;
    auto window_pos = event->scenePosition();
    move(window_pos.x() - press_x, window_pos.y() - press_y);
}

// 鼠标进入，显示快捷方式名称
void AppLNK::enterEvent(QEnterEvent *)
{
    is_change_index = false;
    old_x = x();
    old_y = y();
    QToolTip::showText(mapToGlobal(QPoint(0, -50)), get_name(), this);

    // 变大
    move(x() - SettingHandler::get_instance()->get_border_size(), old_y - SettingHandler::get_instance()->get_border_size());
    set_size(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height());
}

void AppLNK::leaveEvent(QEvent*)
{
    if (! is_change_index)
    {
        // 还原
        move(old_x, old_y);
        set_size(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size());
    }
}

AppLNK::~AppLNK()
{
    qDebug() << "AppINK " << get_name() << " 销毁了";
}

